package com.jikangjun.disk.quoteinfo.control;

import com.alibaba.fastjson.JSON;
import com.jikangjun.disk.mybatis.flexigrid.PagingEntity;
import com.jikangjun.disk.mybatis.flexigrid.TblResponseEntity;
import com.jikangjun.disk.quoteinfo.entity.SearchReceivedShareEntity;
import com.jikangjun.disk.quoteinfo.entity.ReceivedShareEntity;
import com.jikangjun.disk.quoteinfo.service.QuoteinfoService;
import com.jikangjun.disk.spring.control.BaseController;
import com.jikangjun.disk.spring.security.UserDiskEntity;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Date;

/**
 * 收到分享控制器
 */
@Controller
@RequestMapping("/quoteinfo")
public class QuoteinfoController extends BaseController {

    @Resource
    private QuoteinfoService quoteinfoService;

    // 让logger可见
    protected org.apache.logging.log4j.Logger logger = org.apache.logging.log4j.LogManager.getLogger(this.getClass());

    /**
     * 收到分享页面
     */
    @RequestMapping("/page")
    public String quoteinfoPage() {
        return "quoteinfo/quoteinfoPage";
    }

    /**
     * 分页查询收到分享记录
     */
    @RequestMapping(value = "/queryReceivedShareList", method = RequestMethod.POST)
    @ResponseBody
    public String queryReceivedShareList(@RequestParam("page") Integer page,
                                        @RequestParam("pageSize") Integer pageSize,
                                        @RequestParam(value = "fileName", required = false) String fileName,
                                        @RequestParam(value = "sender", required = false) String sender,
                                        @RequestParam(value = "startTime", required = false) String startTime,
                                        @RequestParam(value = "endTime", required = false) String endTime,
                                        @RequestParam(value = "status", required = false) Integer status) {

        try {
            // 获取当前用户
            UserDiskEntity userDiskEntity = (UserDiskEntity) SecurityContextHolder.getContext()
                    .getAuthentication().getPrincipal();

            // 构建查询条件
            SearchReceivedShareEntity entity = new SearchReceivedShareEntity(userDiskEntity.getUserUuid());
            entity.setFileName(fileName);
            entity.setSender(sender);
            entity.setStatus(status);

            // 设置时间范围
            if (startTime != null && !startTime.isEmpty()) {
                entity.setStartTime(parseDate(startTime));
            }
            if (endTime != null && !endTime.isEmpty()) {
                entity.setEndTime(parseDate(endTime));
            }

            // 分页参数
            PagingEntity paging = new PagingEntity();
            paging.setPage(page);
            paging.setRp(pageSize);

            // 查询数据
            TblResponseEntity<ReceivedShareEntity> result = quoteinfoService.queryReceivedShareListByPage(entity, paging);

            return JSON.toJSONString(result);

        } catch (Exception e) {
            logger.error("查询收到分享记录失败", e);
            return failJson("quoteinfo.query.fail");
        }
    }

    /**
     * 接受分享
     */
    @RequestMapping(value = "/acceptShare", method = RequestMethod.POST)
    @ResponseBody
    public String acceptShare(@RequestParam("shareId") String shareId) {
        try {
            quoteinfoService.acceptShare(shareId);
            return successJson("quoteinfo.accept.success");
        } catch (Exception e) {
            logger.error("接受分享失败", e);
            return failJson("quoteinfo.accept.fail");
        }
    }

    /**
     * 批量接受分享
     */
    @RequestMapping(value = "/acceptShareBatch", method = RequestMethod.POST)
    @ResponseBody
    public String acceptShareBatch(@RequestParam("shareIds") String shareIds) {
        try {
            String[] ids = shareIds.split(",");
            quoteinfoService.acceptShareBatch(ids);
            return successJson("quoteinfo.accept.batch.success");
        } catch (Exception e) {
            logger.error("批量接受分享失败", e);
            return failJson("quoteinfo.accept.batch.fail");
        }
    }

    /**
     * 拒绝分享
     */
    @RequestMapping(value = "/rejectShare", method = RequestMethod.POST)
    @ResponseBody
    public String rejectShare(@RequestParam("shareId") String shareId) {
        try {
            quoteinfoService.rejectShare(shareId);
            return successJson("quoteinfo.reject.success");
        } catch (Exception e) {
            logger.error("拒绝分享失败", e);
            return failJson("quoteinfo.reject.fail");
        }
    }

    /**
     * 批量拒绝分享
     */
    @RequestMapping(value = "/rejectShareBatch", method = RequestMethod.POST)
    @ResponseBody
    public String rejectShareBatch(@RequestParam("shareIds") String shareIds) {
        try {
            String[] ids = shareIds.split(",");
            quoteinfoService.rejectShareBatch(ids);
            return successJson("quoteinfo.reject.batch.success");
        } catch (Exception e) {
            logger.error("批量拒绝分享失败", e);
            return failJson("quoteinfo.reject.batch.fail");
        }
    }

    /**
     * 解析日期字符串
     */
    private Date parseDate(String dateStr) {
        try {
            return new java.text.SimpleDateFormat("yyyy-MM-dd").parse(dateStr);
        } catch (Exception e) {
            return null;
        }
    }
} 